/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.service;

import java.util.List;

import javax.swing.plaf.synth.SynthSpinnerUI;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.jiujiudevice.entity.JiuOrderRecord;
import com.boxin.ims.modules.jiujiudevice.dao.JiuOrderRecordDao;

/**
 * 订单记录Service
 * @author sxh
 * @version 2018-08-15
 */
@Component
@Transactional(readOnly = true)
public class JiuOrderRecordService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(JiuOrderRecordService.class);
	
	@Autowired
	private JiuOrderRecordDao jiuOrderRecordDao;
	
	public JiuOrderRecord get(Long id) {
		return jiuOrderRecordDao.findOne(id);
	}
	/**
	 * 查询所有使用中的 订单
	 * @return
	 */
	public List<JiuOrderRecord> findAllUsed(){
		DetachedCriteria dc = jiuOrderRecordDao.createDetachedCriteria();
		dc.add(Restrictions.eq("status", JiuOrderRecord.STATUS_NO_PAY));
		dc.add(Restrictions.isNull("endTime"));
		return jiuOrderRecordDao.find(dc);
	}
	public List<JiuOrderRecord> find (JiuOrderRecord jiuOrderRecord){
		DetachedCriteria dc = jiuOrderRecordDao.createDetachedCriteria();
		
		if(StringUtils.isNotEmpty(jiuOrderRecord.getMemberOpenId())) {
			dc.add(Restrictions.eq("memberOpenId", jiuOrderRecord.getMemberOpenId()));
		}
		
		if(StringUtils.isNotEmpty(jiuOrderRecord.getDeviceSn())) {
			dc.add(Restrictions.eq("deviceSn", jiuOrderRecord.getDeviceSn()));
		}
		
		if(jiuOrderRecord.getStatus() != null) {
			dc.add(Restrictions.eq("status", jiuOrderRecord.getStatus()));
		}
		
		if(jiuOrderRecord.getDelFlag() != null) {
			dc.add(Restrictions.eq("delFlag", jiuOrderRecord.getDelFlag()));
		}
		
		dc.addOrder(Order.desc("id"));
		return jiuOrderRecordDao.find(dc);
	}
	/**
	 * 查询用户待支付订单
	 * @param jiuOrderRecord
	 * @return
	 */
	public List<JiuOrderRecord> queryUnpaidOrderByMember (Long memberId){
		DetachedCriteria dc = jiuOrderRecordDao.createDetachedCriteria();
		
		dc.add(Restrictions.eq("memberId", memberId));
		dc.add(Restrictions.eq("status", JiuOrderRecord.STATUS_NO_PAY));
		dc.add(Restrictions.eq("delFlag", JiuOrderRecord.DEL_NO));
		dc.add(Restrictions.isNotNull("endTime"));
		dc.addOrder(Order.desc("id"));
		return jiuOrderRecordDao.find(dc);
	}
	public Page<JiuOrderRecord> find(Page<JiuOrderRecord> page, JiuOrderRecord jiuOrderRecord) {
		DetachedCriteria dc = jiuOrderRecordDao.createDetachedCriteria();
		if (jiuOrderRecord.getId()!=null && jiuOrderRecord.getId()>0){
			dc.add(Restrictions.eq("id", jiuOrderRecord.getId()));
		}
		
		if (StringUtils.isNotEmpty(jiuOrderRecord.getMemberMobile())){
			dc.add(Restrictions.eq("memberMobile", jiuOrderRecord.getMemberMobile()));
		}
		if (StringUtils.isNotEmpty(jiuOrderRecord.getMemberOpenId())){
			dc.add(Restrictions.eq("memberOpenId", jiuOrderRecord.getMemberOpenId()));
		}
		if (StringUtils.isNotEmpty(jiuOrderRecord.getOrderNo())){
			dc.add(Restrictions.eq("orderNo", jiuOrderRecord.getOrderNo()));
		}
		if (StringUtils.isNotEmpty(jiuOrderRecord.getDeviceSn())){
			dc.add(Restrictions.eq("deviceSn",jiuOrderRecord.getDeviceSn() ));
		}
		if (StringUtils.isNotEmpty(jiuOrderRecord.getCompanyName())){
			dc.add(Restrictions.like("companyName","%"+jiuOrderRecord.getCompanyName()+"%" ));
		}
		if(null != jiuOrderRecord.getStatus()){
			dc.add(Restrictions.eq("status",jiuOrderRecord.getStatus()));
		}
		if(null != jiuOrderRecord.getDelFlag()){
			dc.add(Restrictions.eq("delFlag",jiuOrderRecord.getDelFlag()));
		}
		dc.addOrder(Order.desc("id"));
		return jiuOrderRecordDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public JiuOrderRecord save(JiuOrderRecord jiuOrderRecord) {
		return jiuOrderRecordDao.save(jiuOrderRecord);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		jiuOrderRecordDao.deleteById(id);
	}

	public void delete(JiuOrderRecord jiuOrderRecord) {
		jiuOrderRecordDao.delete(jiuOrderRecord);
	}

	/**
	 * 
	 * @function: <p>根据订单号查询订单 </p>
	 * @param orderNo
	 * @return
	 */
	public JiuOrderRecord getByOrderNo(String orderNo) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = jiuOrderRecordDao.createDetachedCriteria();
		dc.add(Restrictions.eq("orderNo", orderNo));
		List<JiuOrderRecord> oList  = jiuOrderRecordDao.find(dc) ;
		return oList!= null && oList.size() >0  ? oList.get(0) : null ;
	}

	/**
	 * 
	 * @function: <p>根据订单状态查询订单 </p>
	 * @param param
	 * @return
	 */
	public Long findAllCount(JiuOrderRecord jiuOrderRecord ) {
		// TODO Auto-generated method stub
	       DetachedCriteria dc = jiuOrderRecordDao.createDetachedCriteria();
	       if(null != jiuOrderRecord.getStatus()) {
		       dc.add(Restrictions.eq("status", jiuOrderRecord.getStatus()));
	       }
        dc.add(Restrictions.eq("memberOpenId", jiuOrderRecord.getMemberOpenId()));
		return jiuOrderRecordDao.count(dc);
	}
	/**
	 * 
	 * @function: <p>查询所有的订单，删除金额为0的订单 </p>
	 * @return
	 */
	public List<JiuOrderRecord> findAll() {
		// TODO Auto-generated method stub
		DetachedCriteria dc = jiuOrderRecordDao.createDetachedCriteria();
		List<JiuOrderRecord> oList  = jiuOrderRecordDao.find(dc) ;
		return oList;
	}
	
	
}
